package xw.qxypt.modules.baseinfo.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import xw.qxypt.common.utils.BaseTree;
import xw.qxypt.common.utils.Constant;
import xw.qxypt.common.utils.PageUtils;
import xw.qxypt.common.utils.Query;

import xw.qxypt.modules.baseinfo.dao.LhpEquipmentDao;
import xw.qxypt.modules.baseinfo.entity.LhpEquipmentEntity;
import xw.qxypt.modules.baseinfo.entity.LhpHighwaySectionEntity;
import xw.qxypt.modules.baseinfo.entity.LhpInstallationPointEntity;
import xw.qxypt.modules.baseinfo.service.LhpEquipmentService;
import xw.qxypt.modules.baseinfo.service.LhpHighwaySectionService;
import xw.qxypt.modules.baseinfo.service.LhpInstallationPointService;


@Service("lhpEquipmentService")
public class LhpEquipmentServiceImpl extends ServiceImpl<LhpEquipmentDao, LhpEquipmentEntity> implements LhpEquipmentService {
    @Autowired
    private LhpInstallationPointService lhpInstallationPointService;
    @Autowired
    private LhpHighwaySectionService lhpHighwaySectionService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String equipmentName= (String) params.get("equipmentName");
        String equipmentStatus= (String) params.get("equipmentStatus");
        IPage<LhpEquipmentEntity> page = this.page(
                new Query<LhpEquipmentEntity>().getPage(params),
                new QueryWrapper<LhpEquipmentEntity>()
                        .like(StringUtils.isNotBlank(equipmentName),"equipment_name", equipmentName)
                        .eq(StringUtils.isNotBlank(equipmentStatus),"equipment_status", equipmentStatus)
                        .apply(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER))
        );
        List<LhpEquipmentEntity> equipmetlist=page.getRecords();
        List<LhpInstallationPointEntity> pointList=lhpInstallationPointService.list();
        for(LhpEquipmentEntity equipmentEntity:equipmetlist){
            for(LhpInstallationPointEntity pointEntity:pointList){
                if(equipmentEntity.getPointId().equals(pointEntity.getId())){
                    equipmentEntity.setPointName(pointEntity.getPointName());
                }
            }
        }
        return new PageUtils(page);
    }

    @Override
    public List<BaseTree> equipmentTree() {
        List<LhpHighwaySectionEntity> waysectList =lhpHighwaySectionService.list();
        List<LhpInstallationPointEntity> pointList=lhpInstallationPointService.list();
        List<LhpEquipmentEntity> equipmentList=this.list();
        List<BaseTree> treeList=new ArrayList<BaseTree>();
        for (LhpHighwaySectionEntity waysect:waysectList){
            boolean waysectbl=true;
            for (LhpInstallationPointEntity point:pointList){
                boolean pointbl=true;
                if(point.getWaysectId().equals(waysect.getId())){
                    if(waysectbl){
                        BaseTree tree=new BaseTree();
                        tree.setId(waysect.getId());
                        tree.setName(waysect.getRoadName());
                        treeList.add(tree);
                        waysectbl=false;
                    }
                    for (LhpEquipmentEntity equipment:equipmentList ){
                        if(equipment.getPointId().equals(point.getId())){
                            if(pointbl){
                                BaseTree tree=new BaseTree();
                                tree.setId(point.getId());
                                tree.setName(point.getPointName());
                                tree.setParentId(point.getWaysectId());
                                treeList.add(tree);
                                pointbl=false;
                            }
                            BaseTree tree=new BaseTree();
                            tree.setId(equipment.getId());
                            tree.setName(equipment.getEquipmentName());
                            tree.setParentId(equipment.getPointId());
                            treeList.add(tree);
                            for (int i=1;i<=5;i++){
                                BaseTree ltree=new BaseTree();
                                ltree.setId(equipment.getId()+"-"+i);
                                ltree.setName(i+"级");
                                ltree.setParentId(equipment.getId());
                                treeList.add(ltree);
                            }
                        }
                    }
                }
            }
        }
        return treeList;
    }


    @Override
    public List<LhpEquipmentEntity> getEquipmentByUserId(Long userId) {
        //查询用户下安装点
        List<LhpInstallationPointEntity> pointList=lhpInstallationPointService.getPointByUserId(userId);
        List<String> idList=new ArrayList<>();
        for (LhpInstallationPointEntity pointEntity:pointList) {
            idList.add(pointEntity.getId());
        }
        //根据pointid查询设备
        List<LhpEquipmentEntity> equipmentList=this.list(
                new QueryWrapper<LhpEquipmentEntity>()
                        .in("point_id", idList)
        );
        return equipmentList;
    }

    @Override
    public List<LhpEquipmentEntity> getEqupimentByPointId(String pointId) {
        List<LhpEquipmentEntity> equipmentList=this.list(
                new QueryWrapper<LhpEquipmentEntity>()
                        .eq(StringUtils.isNotBlank(pointId),"point_id", pointId)
        );
        return equipmentList;
    }

}
